package com.dream.service;

import com.dream.common.constans.ErrorCode;
import com.dream.common.constans.RedisKeysDefine;
import com.dream.common.utils.ActionUtils;
import com.dream.common.utils.JsonUtils;
import com.dream.common.utils.ThreadContextUtil;
import com.dream.dto.DataResponse;
import com.dream.dto.console.CacheUser;
import com.dream.dto.request.GroupMemberBatchRequest;
import com.dream.entity.GroupMember;
import com.dream.mapper.GroupMemberMapper;
import com.dream.service.basic.RedisCacheService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * @description:
 * @author: bushuai
 * @date: 2019/11/26 22:11
 */
@Service
public class ProcedureServiceImpl {

    @Resource
    private RedisCacheService redisCacheService;
    @Resource
    private GroupMemberMapper groupMemberMapper;

    /**
     * 检测token有效性
     *
     * @param request
     * @param response
     * @return
     */
    public boolean tokenValidate(HttpServletRequest request, HttpServletResponse response) {
//        String ticket = request.getHeader("ticket");
//        if (ticket == null) ticket = request.getParameter("ticket");
//
//        if (StringUtils.isEmpty(ticket)) {
//            ActionUtils.printWriter(response, DataResponse.builderFailed(ErrorCode.auth_error));
//            return false;
//        }
//
//        String cacheVal = redisCacheService.get(RedisKeysDefine.LOGIN_TOKEN.concat(ticket));
//        if (StringUtils.isEmpty(cacheVal)) {
//            ActionUtils.printWriter(response, DataResponse.builderFailed(ErrorCode.account_check_error));
//            return false;
//        }
//
//        CacheUser cacheUser = JsonUtils.parseObject(cacheVal, CacheUser.class);
//        if(cacheUser == null){
//            ActionUtils.printWriter(response, DataResponse.builderFailed("covert cache login token error."));
//            return false;
//        }

//        ThreadContextUtil.put("userName", cacheUser.user);
        return true;
    }


    /**
     * 获取该用户所有好友角色
     *
     * @param userName 账户
     * @return 好友角色列表
     */
    public GroupMemberBatchRequest getMemberRoles(String userName) {
        GroupMemberBatchRequest batchRequest = redisCacheService.getObject(RedisKeysDefine.USER_ROLES.concat(userName), GroupMemberBatchRequest.class);
        if (batchRequest != null) {
            return batchRequest;
        }

        batchRequest = new GroupMemberBatchRequest();
        List<GroupMember> members = groupMemberMapper.select(GroupMember.builder().userName(userName).build());
        batchRequest.userName = userName;
        List<String> touchPackList = new ArrayList<>();
        List<String> managerList = new ArrayList<>();
        List<String> blackList = new ArrayList<>();
        List<String> insideUserList = new ArrayList<>();

        //[touchPackList摸包号,managerList管理员,blackList黑名単,insideUserList托号]
        members.forEach(e -> {
            if (e.isTouchPack != null && e.isTouchPack == 1) {
                touchPackList.add(e.friendId);
            }
            if (e.isManager != null && e.isManager == 1) {
                managerList.add(e.friendId);
            }
            if (e.isInsideUser != null && e.isInsideUser == 1) {
                insideUserList.add(e.friendId);
            }
            if (e.isBlackList != null && e.isBlackList == 1) {
                blackList.add(e.friendId);
            }
        });

        batchRequest.roleMap.put("touchPackList", touchPackList);
        batchRequest.roleMap.put("managerList", managerList);
        batchRequest.roleMap.put("blackList", blackList);
        batchRequest.roleMap.put("insideUserList", insideUserList);

        redisCacheService.setObject(RedisKeysDefine.USER_ROLES.concat(userName), batchRequest);
        return batchRequest;
    }

}
